Open
Conversation
Author
|
What do I have to do in order to get this merged? |
Owner
|
Does this follow Rednet exactly? I think somethings do need to be clarified a bit such as what channel it is on and how to use it rather than a fairly broad outline. |
Author
|
I read it out of the rednet API source code. |
Author
|
The source code from the rednet API: lookup function: function lookup( sProtocol, sHostname )
if type( sProtocol ) ~= "string" then
error( "expected string", 2 )
end
-- Build list of host IDs
local tResults = nil
if sHostname == nil then
tResults = {}
end
-- Check localhost first
if tHostnames[ sProtocol ] then
if sHostname == nil then
table.insert( tResults, os.getComputerID() )
elseif sHostname == "localhost" or sHostname == tHostnames[ sProtocol ] then
return os.getComputerID()
end
end
if not isOpen() then
if tResults then
return table.unpack( tResults )
end
return nil
end
-- Broadcast a lookup packet
broadcast( {
sType = "lookup",
sProtocol = sProtocol,
sHostname = sHostname,
}, "dns" )
-- Start a timer
local timer = os.startTimer( 2 )
-- Wait for events
while true do
local event, p1, p2, p3 = os.pullEvent()
if event == "rednet_message" then
-- Got a rednet message, check if it's the response to our request
local nSenderID, tMessage, sMessageProtocol = p1, p2, p3
if sMessageProtocol == "dns" and type(tMessage) == "table" and tMessage.sType == "lookup response" then
if tMessage.sProtocol == sProtocol then
if sHostname == nil then
table.insert( tResults, nSenderID )
elseif tMessage.sHostname == sHostname then
return nSenderID
end
end
end
else
-- Got a timer event, check it's the end of our timeout
if p1 == timer then
break
end
end
end
if tResults then
return table.unpack( tResults )
end
return nil
endhosting mechanism: local tHostnames = {}
function host( sProtocol, sHostname )
if type( sProtocol ) ~= "string" or type( sHostname ) ~= "string" then
error( "expected string, string", 2 )
end
if sHostname == "localhost" then
error( "Reserved hostname", 2 )
end
if tHostnames[ sProtocol ] ~= sHostname then
if lookup( sProtocol, sHostname ) ~= nil then
error( "Hostname in use", 2 )
end
tHostnames[ sProtocol ] = sHostname
end
end
function unhost( sProtocol )
if type( sProtocol ) ~= "string" then
error( "expected string", 2 )
end
tHostnames[ sProtocol ] = nil
endpart of run function dedicated to dns: elseif sEvent == "rednet_message" then
-- Got a rednet message (queued from above), respond to dns lookup
local nSenderID, tMessage, sProtocol = p1, p2, p3
if sProtocol == "dns" and type(tMessage) == "table" and tMessage.sType == "lookup" then
local sHostname = tHostnames[ tMessage.sProtocol ]
if sHostname ~= nil and (tMessage.sHostname == nil or tMessage.sHostname == sHostname) then
rednet.send( nSenderID, {
sType = "lookup response",
sHostname = sHostname,
sProtocol = tMessage.sProtocol,
}, "dns" )
end
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is adding a DNS Protocol Description according to the Rednet API.